package cn.wlinker.video.monitor.controller;


import cn.hutool.core.date.DateUtil;
import cn.wlinker.video.monitor.brand.hikversion.HikVisionAccessPlatformEnum;
import cn.wlinker.video.monitor.brand.hikversion.create.net.aop.HikCameraAspect;
import cn.wlinker.video.monitor.brand.hikversion.utils.NetDeviceCacheUtils;
import cn.wlinker.video.monitor.domain.R;
import cn.wlinker.video.monitor.domain.dto.MonitorDeviceDTO;
import cn.wlinker.video.monitor.enums.FileTypeEnum;
import cn.wlinker.video.monitor.utils.FilePathUtils;
import cn.wlinker.video.monitor.utils.FileUploadUtils;
import cn.wlinker.video.monitor.utils.VideoKeyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 文件上传下载
 *
 * @author gxsjx
 * @version 1.0
 * @date 2025/5/24
 * Copyright © wlinker.cn
 */
@Slf4j
@Api(tags = "文件上传下载")
@RestController
@RequestMapping("/api/file")
public class UploadController {

    @Resource
    HikCameraAspect aspect;

    @GetMapping(value = "/hikNet/downloadVideo")
    @ApiOperation("下载录像-海康网络设备")
    public R<String> downloadVideo(@RequestParam String id, @RequestParam String startTime, @RequestParam String endTime) {
        MonitorDeviceDTO monitorDeviceDTO = VideoKeyUtils.getMonitorDeviceDtoById(id);
        String deviceCode = monitorDeviceDTO.getDeviceCode();
        String accessPlatform = monitorDeviceDTO.getAccessPlatform();
        if (!HikVisionAccessPlatformEnum.net.getAccessPlatform().equals(accessPlatform)) {
            return R.err("不支持该平台");
        }
        aspect.logout(deviceCode);
        aspect.login(deviceCode);
        LocalDateTime startLocalTime = DateUtil.parseLocalDateTime(startTime);
        LocalDateTime endLocalTime = DateUtil.parseLocalDateTime(endTime);
        //单次录像下载时长范围不能超过一小时
        if (startLocalTime.isAfter(endLocalTime)) {
            return R.err("开始时间不能大于结束时间");
        }
        if (startLocalTime.plusHours(1).isBefore(endLocalTime)) {
            return R.err("下载时间不能超过一小时");
        }
        String randomPath = FilePathUtils.getRandomNameWithPath(FileTypeEnum.mp4);
        Integer channelNo = monitorDeviceDTO.getChannelNo();
        NetDeviceCacheUtils.getHcNetTools(deviceCode).downloadBack(startLocalTime, endLocalTime, randomPath, channelNo);
        String urlPathByFileName = FilePathUtils.getUrlPathByFileName(randomPath);
        return R.data(urlPathByFileName);
    }

    /**
     * 上传多个文件
     *
     * @param files 上传文件集合
     * @return
     */
    @PostMapping(value = "/upload")
    @ApiOperation("上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件", dataType = "__file", required = true),
    })
    public R<List<String>> upload(MultipartFile[] files) throws IOException {
        List<String> upload = FileUploadUtils.upload(files);
        return R.data(upload);
    }
}
